package com.example.myblog.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.myblog.common.lang.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class IndexController extends BaseController {

    @RequestMapping({"/","index",""})
    public String index(){
        //查询帖子，参数调用了getPage()方法，获得分页信息
        IPage results = postService.paging(getPage(),null,null,null,null,"created");
        request.setAttribute("pageData",results);
        request.setAttribute("currentCategoryId",0);
        return "index";
    }

    @RequestMapping("/search")
    public String search(String q, String pn, HttpServletRequest request){

        if (q == null)
            q=(String)request.getSession().getAttribute("q");
        IPage pageData = searchService.search(getPage(), q);
        request.getSession().setAttribute("q",q);
        request.setAttribute("currentCategoryId",0);
        request.setAttribute("pageData",pageData);
        return "search";
    }


}
